<?php

namespace application\app\controller;

use ticky\controller;

class index extends controller {

    public $config;
    public $dbprefix = 'tky_';

    public function __construct() {
        $this->config = get_config();
        $this->dbprefix = db()->prefix;
        $this->assign('config', $this->config);
        //设置手机模块模板风格
        set_module_theme('default');
        $meun = db('category')->field('catname,catid,type,pclink')->where(array('parentid' => 0))->order('sort')->select();
        $this->assign('meuns', $meun);
    }

    /**
     * 首页
     */
    public function index() {
        $this->display('index');
    }

    /**
     * 栏目列表页
     */
    public function lists() {
        $catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
        if (!$catid)
            showmsg(L('missing_parameter'), 'stop');
        $catinfo = get_category($catid);
        if (!$catinfo) {
            showmsg(L('category_not_existent'), 'stop');
        }
        extract($catinfo);
        //外部链接
        if ($catinfo['type'] == 2) {
            showmsg(L('is_jump'), $catinfo['pclink'], 1);
        }
        $modelid = $catinfo['modelid'];
        $template = $catid == $arrchildid ? $list_template : $category_template;
        //单页面
        if ($catinfo['type'] == 1) {
            $r = db('page')->where(array('catid' => $catid))->find();
            $r['catname'] = $catinfo['catname'];
            $r['template'] = $template;
            $this->assign('item', $r);
            $template = $catinfo['category_template'];
        } else {
            $modelinfo = get_modelinfo();
            $tablename = $modelinfo[$modelid]['tablename'];
            $arrchildid = $catinfo['arrchildid'];
            $catids = strpos($arrchildid, ',') ? ' AND catid IN (' . $arrchildid . ')' : ' AND catid=' . $arrchildid;
            $where = 'status=1' . $catids;
            $ret = db('content a')
                    ->join($this->dbprefix . '' . $tablename . " g on a.contentid=g.contentid")
                    ->where($where)
                    ->order('views DESC')
                    ->newpage();
            foreach ($ret['items'] as $row) {
                $thumb = $row['thumb'];
                if (empty($thumb)) {
                    $thumb = "/static/home/nophoto.gif";
                }
                $row['thumb'] = $thumb;
                $row['description'] = cutstr($row['description'], 310);
                $row['content'] = '';

                $row['catname'] = $catinfo['catname'];
                $row['modelname'] = $tablename;
                $row['template'] = $template;
                $data[] = $row;
            }
            $this->assign('page', $ret['page']);
            $this->assign('items', $data);
        }
        //debug($data);
        $title = $catname;
        $this->assign('catid', $catid);
        $this->assign('catinfo', $catinfo);
        $this->assign('template', $template);
        $this->assign('title', $title);
        $this->display($template);
    }

    /**
     * 内容页
     */
    public function show() {
        $catid = isset($_GET['catid']) ? intval($_GET['catid']) : 0;
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$catid || !$id) {
            showmsg(L('missing_parameter'), 'stop');
        }
        $catinfo = get_category($catid);
        if (!$catinfo) {
            showmsg(L('category_not_existent'), 'stop');
        }
        $modelid = $catinfo['modelid'];
        $template = $catinfo['show_template'];

        $modelinfo = get_modelinfo();
        if (!isset($modelinfo[$modelid])) {
            showmsg(L('model_not_existent'), 'stop');
        }
        $table = $modelinfo[$modelid]['tablename'];
        $where = ['a.contentid' => $id];

        $data = db('content a')
                ->field('*')
                ->join($this->dbprefix . '' . $table . " g on a.contentid=g.contentid")
                ->where($where)
                ->find();

        if (!$data || $data['status'] != 1) {
            showmsg(L('content_not_existent'), 'stop');
        }

        if (empty($data['thumb'])) {
            $data['thumb'] = "/static/home/nophoto.gif";
        }
        //更新点击量
        db('content')->where(['contentid' => $id])->update('views = views + 1');
        // debug($data);
        $this->assign('item', $data);
        $this->assign('catid', $catid);
        $this->assign('template', $template);
        $this->display($template);
    }

}
